function generateModelDeriv_file(allModelParams,x1,y,strucOfArrays,positionFile,nameOfFunction)
% This function generates an M function which computes the derivatives of the model
% as needed to solve the DSGE model by the Extended Path, 
% The results are saved in the file nameOfFunction, for instance 
% nameOfFunction = 'getModelDeriv.m';
% In this version of generateModelDeriv_file we write the output directly
% to the file.

% We start deleting the old version of the file - if it exists
saveFile = [positionFile,'\',nameOfFunction];
if exist(saveFile,'file') > 0
    delete(saveFile)
end

text = ['function [f,fx11,fx11p,fy,fyp,fx12] = ',nameOfFunction(1:end-2),'(nx,mx,myx,ny,params,N,h0,g0,x,y,t,getDeriv)'];
dlmwrite(saveFile,text,'-append','delimiter','');
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '%Unfold params';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(allModelParams)
    text = [allModelParams{i}, '= params.',allModelParams{i},';'];
    dlmwrite(saveFile,text,'-append','delimiter','');
end 
text = ' ';
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% Current values in the model;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% x(N,mx) y(N,ny);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% x;';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(x1)
     text = [char(x1(i)),' = x(t,', num2str(i),')+h0(',num2str(i),',1);'];
     dlmwrite(saveFile,text,'-append','delimiter','');
end
text = '% xp;';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(x1)
     text = [char(x1(i)),'p',' = x(t+1,', num2str(i),')+h0(',num2str(i),',1);'];
     dlmwrite(saveFile,text,'-append','delimiter','');
end
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '% y';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(y)
     text = [char(y(i)),' = y(t,', num2str(i),')+g0(',num2str(i),',1);';];
     dlmwrite(saveFile,text,'-append','delimiter','');
end
text = '% yp;';
dlmwrite(saveFile,text,'-append','delimiter','');
for i=1:length(y)
     text = [char(y(i)),'p',' = y(t+1,', num2str(i),')+g0(',num2str(i),',1);',];
     dlmwrite(saveFile,text,'-append','delimiter','');
end
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '%% Setting the dimension of the matrices ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'fx11  = zeros(ny+mx,mx);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'fx11p = zeros(ny+mx,mx);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'fy   = zeros(mx+ny,ny);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'fyp  = zeros(mx+ny,ny);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'fx12 = zeros(mx+ny,myx);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '%% f Function evaluation';
dlmwrite(saveFile,text,'-append','delimiter','');
DispSymMatrixMatlab_file(strucOfArrays.('f'),'f',saveFile);
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '%% Model derivatives fx11, fx11p, fy, fyp';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'if getDeriv == 1';
dlmwrite(saveFile,text,'-append','delimiter','');
DispSymMatrixMatlab_file(strucOfArrays.('fx11'),'fx11',saveFile);
text = 'if t == 1;';
dlmwrite(saveFile,text,'-append','delimiter','');
text = '    fx11  = zeros(ny+mx,mx);';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'end';
dlmwrite(saveFile,text,'-append','delimiter','');
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
DispSymMatrixMatlab_file(strucOfArrays.('fx11p'),'fx11p',saveFile);
dlmwrite(saveFile,text,'-append','delimiter','');
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
DispSymMatrixMatlab_file(strucOfArrays.('fy'),'fy',saveFile);
text = ' ';
dlmwrite(saveFile,text,'-append','delimiter','');
DispSymMatrixMatlab_file(strucOfArrays.('fx12'),'fx12',saveFile);
text = 'if t < N';
dlmwrite(saveFile,text,'-append','delimiter','');
DispSymMatrixMatlab_file(strucOfArrays.('fyp'),'fyp',saveFile);
text = 'end';
dlmwrite(saveFile,text,'-append','delimiter','');
text = 'end';
dlmwrite(saveFile,text,'-append','delimiter','');
end





